package com.ruoyi.project.cms.warehouse.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.warehouse.domain.CmsStockGoods;
import com.ruoyi.project.cms.warehouse.domain.CmsStockInfo;
import com.ruoyi.project.cms.warehouse.service.IGoodsService;
import com.ruoyi.project.cms.warehouse.service.IInfoService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("cms/warehouse/goods")
public class WarehouseGoodsController extends BaseController {

    final private String prefix = "cms/warehouse/goods";


    @Autowired
    private IGoodsService iGoodsService;

    @Autowired
    private IInfoService iInfoService;

    @GetMapping("/goods")
    public String getGoods(){
        return prefix+"/goods";
    }


    /**
     * 商品列表展示
     * @param goods
     * @return
     */

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo getGoodsList(CmsStockGoods goods){
        startPage();
        List<CmsStockGoods> cmsStockGoods = iGoodsService.selectAll(goods);
        return  getDataTable(cmsStockGoods);
    }


    /**
     * 引入新进货物
     * @return
     */

    @GetMapping("/add")
    public String getAdd(){
        return prefix+"/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated CmsStockGoods goods)
    {
        Integer count = goods.getCount();
        if (count>=10) {
            goods.setType("1");
        }else if (count<10&&count>0){
            goods.setType("2");
        }else{
            goods.setType("3");
        }
        int i = iGoodsService.addGoods(goods);
        WarehouseGoodsController g = new WarehouseGoodsController();
        CmsStockInfo info = g.createInfo(goods, "新进");
        iInfoService.addInfo(info);
        return toAjax(i);
    }


    /**
     * 校验商品名称
     */

    @PostMapping("/checkPostNameUnique")
    @ResponseBody
    public String checkPostNameUnique(String name)
    {
        return iGoodsService.findByName(name);

    }

    /**
     * 校验商品编码
     */
    @PostMapping("/checkPostCodeUnique")
    @ResponseBody
    public String checkPostCodeUnique(String productCode)
    {
        return iGoodsService.finByProductCode(productCode);
    }

    /**
     * 编辑商品 信息
     * @param id
     * @param mmap
     * @return
     */

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("goods", iGoodsService.findByProductCode(id));
        System.out.println(mmap.get("goods"));
        return prefix + "/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CmsStockGoods goods)
    {
        return toAjax(iGoodsService.updateGoods(goods));
    }

    /**
     * 删除 商品信息
     * @param ids
     * @return
     */

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            int i = iGoodsService.deleteByids(ids);
            return toAjax(i);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


    /**
     * 采购订单处理
     * @param id
     * @param mmap
     * @return
     */

    @GetMapping("/purchase/{id}")
    public String purchase(@PathVariable("id") String id, ModelMap mmap)
    {
        CmsStockGoods good = iGoodsService.findByProductCode(id);
        good.setCount(null);
        mmap.put("goods", good);
        return prefix + "/purchase";
    }


    @PostMapping("/purchase")
    @ResponseBody
    public AjaxResult purchase(@Validated CmsStockGoods goods)
    {
        WarehouseGoodsController g = new WarehouseGoodsController();
        CmsStockInfo info = g.createInfo(goods, "新进");
        iInfoService.addInfo(info);

        CmsStockGoods good = iGoodsService.findByProductCode(goods.getProductCode());
        Integer count = good.getCount();
        Integer countAll= goods.getCount()+count;
        goods.setCount(countAll);
        int i = iGoodsService.updateGoods(goods);
        return toAjax(i);
    }

    /**
     *   出货 订单处理
     * @param id
     * @param mmap
     * @return
     */

    @GetMapping("/outbound/{id}")
    public String outbound(@PathVariable("id") String id, ModelMap mmap)
    {
        CmsStockGoods good = iGoodsService.findByProductCode(id);
        good.setCount(null);
        mmap.put("goods", good);
        return prefix + "/outbound";
    }


    @PostMapping("/checkCountUnique")
    @ResponseBody
    public String checkCountUnique(String productCode,Integer count)
    {
        System.out.println(productCode+" "+count);
        CmsStockGoods good = iGoodsService.findByProductCode(productCode);
        if (good.getCount()<count){
            return "1";
        }
        return "0";

    }

    @PostMapping("/outbound")
    @ResponseBody
    public AjaxResult outbound(@Validated CmsStockGoods goods)
    {
        WarehouseGoodsController g = new WarehouseGoodsController();
        CmsStockInfo info = g.createInfo(goods, "出仓");
        iInfoService.addInfo(info);

        CmsStockGoods good = iGoodsService.findByProductCode(goods.getProductCode());
        Integer count =good.getCount()-goods.getCount();
        goods.setCount(count);
        int i = iGoodsService.updateGoods(goods);
        return toAjax(i);
    }




    private  CmsStockInfo createInfo(CmsStockGoods goods,String type){
        CmsStockInfo cmsStockInfo = new CmsStockInfo();
        cmsStockInfo.setProductCode(goods.getProductCode());
        cmsStockInfo.setProductName(goods.getName());
        cmsStockInfo.setProductCount(goods.getCount()+"");
        cmsStockInfo.setDesc(goods.getDesc());
        User sysUser = getSysUser();
        @NotBlank(message = "登录账号不能为空") @Size(min = 0, max = 30, message = "登录账号长度不能超过30个字符") String loginName = sysUser.getLoginName();
        cmsStockInfo.setCreateUser(loginName);
        String time = DateUtils.getTime();
        Date date = DateUtils.parseDate(time);
        cmsStockInfo.setCreateTime(date);
        cmsStockInfo.setType(type);
        return cmsStockInfo;
    }

}
